<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/message.jsp"%>
<%!
	// 修改密码
	public Message password(Token token, Map<String, String> parameters) {
		Message msg = new Message();
		String oldPassword = Tools.getDesc(parameters.get("old_password"), "");
		String password = Tools.getDesc(parameters.get("password"), "-9999");
		String rePassword = Tools.getDesc(parameters.get("repassword"), "-99999");
		//两次密码不一致
		if (!password.equals(rePassword)) {
			return new Message(30037, "两次密码不一致");
		}
		List<Token> list = QBuilder.db("basedb").queryHql("from Token where hospital = "+ token.getHospital() +" and token = '" + token.getToken() + "'");
		if (list.size() == 0) {
			return new Message(30022, "用户不存在");
		}
		if (!oldPassword.equals(token.getPassword())) {
			return new Message(30023, "原密码错误");
		}
		String match = "[a-zA-Z0-9_]*";
		if (password.length() < 6) { //密码长度不合法
			return new Message(30025, "密码长度不少于6个字符");
		}
		if (!password.matches(match)) { //密码不能包含非法字符
			return new Message(30026, "密码包含非法字符");
		}
		token = list.get(0);
		token.setPassword(password);
		if(token.getJsonString() == null){
		    token.setJsonString("{}");
		}
		// 修改token中的密码
		DBTools.db("basedb").modItem(token, token.getId());
		List<User> users = QBuilder.db("basedb").queryHql("from User where id = " + token.getUid());
		if (users.size() == 0) {
			return new Message(30022, "用户不存在");
		}
		User user = users.get(0);
		user.setPassword(password);
		// 修改user中的密码
		DBTools.db("basedb").modItem(user, user.getId());
		// 修改密码后删除其他设备登录的token
		QBuilder.db("basedb").execHQL("delete from Token where hospital = "+ token.getHospital() +" and uid = " + token.getUid() + " and token !='" + token.getToken() + "'");
		String content = String.format("您于%s将密码重置成功", new Object[]{DateUtil.formatDateByFormat(new Date(), DateUtil.DATETIME_FORMAT)});
		createMsg(token.getUid(), token.getHospital(), "密码重置通知", "系统通知", content, SYSTEMINFO);
		return msg;
	}
%>
<%
    Tools.encryptPrint(out, password(token, parameters));
%>
